<template>
  <el-dialog :title="form.id ? '修改角色' : '新增角色'" :visible="showDialog" :show-close="false" @close="close">
    <el-form ref="form" :rules="rules" :model="form">
      <el-form-item label="角色名称" prop="name" label-width="80px">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description" label-width="80px">
        <el-input v-model="form.description" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getRoleDetail, addRole, updataRole } from '@/api/roles'

export default {
  name: 'AddRole',
  props: {
    showDialog: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {},
      rules: {
        name: [{ required: true, message: '请输入角色名', trigger: 'blur' }],
        description: [{ required: true, message: '请输入描述', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getDetail(id) {
      const res = await getRoleDetail(id)
      this.form = res.data
    },
    close() {
      this.form = {}
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    save() {
      this.$refs.form.validate(async v => {
        if (v) {
          this.form.id ? await updataRole(this.form) : await addRole(this.form)
          const isAdd = !this.form.id
          this.close()
          this.$emit('reload', isAdd)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
